package org.jeecg.modules.tt_question_collect.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.puppycrawl.tools.checkstyle.gui.Main;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.functions.ConstantFunction.False;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.ComboModel;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.api.controller.SystemAPIController;
import org.jeecg.modules.system.controller.SysFillRuleController;
import org.jeecg.modules.tt_question_collect.entity.TtPlanConfirm;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionCollect;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionDealRecord;
import org.jeecg.modules.tt_question_collect.entity.tspmodel;
import org.jeecg.modules.tt_question_collect.service.ITtAuditRecordService;
import org.jeecg.modules.tt_question_collect.service.ITtPlanConfirmService;
import org.jeecg.modules.tt_question_collect.service.ITtQuestionCollectService;
import org.jeecg.modules.tt_question_collect.util.TransUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 问题收集表
 * @Author: jeecg-boot
 * @Date: 2021-01-29
 * @Version: V1.0
 */
@Api(tags = "问题收集表")
@RestController
@RequestMapping("/tt_question_collect/ttQuestionCollect")
@Slf4j
public class TtQuestionCollectController extends JeecgController<TtQuestionCollect, ITtQuestionCollectService> {
    @Autowired
    private ITtQuestionCollectService ttQuestionCollectService;

    @Autowired
    private ITtAuditRecordService ttAuditRecordService;

    @Autowired
    private  SystemAPIController systemAPIController;

    @Autowired
    private ITtPlanConfirmService ttPlanConfirmService;


    /**
     * 分页列表查询
     *
     * @param ttQuestionCollect
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "问题收集表-分页列表查询")
    @ApiOperation(value = "问题收集表-分页列表查询", notes = "问题收集表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<Page<TtQuestionCollect>> queryPageList(TtQuestionCollect ttQuestionCollect,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        Result<Page<TtQuestionCollect>> result = new Result<>();
        Page<TtQuestionCollect> pageList = new Page<TtQuestionCollect>(pageNo, pageSize);
        ttQuestionCollect.setBy2(req.getParameter("isme")) ;
        ttQuestionCollect.setBy3(sysUser.getId());
        System.out.println(req.getParameter("count"));

        pageList = ttQuestionCollectService.queryquestionlist(pageList,ttQuestionCollect);
        result.setResult(pageList);
        return result;
    }

    /**
     * @return
     */
    @AutoLog(value = "获取当前登录用户归属部门流程id")
    @ApiOperation(value = "获取当前登录用户归属部门流程id", notes = "获取当前登录用户归属部门流程id")
    @GetMapping(value = "/querybmlcid")
    public Result<?> querybmlcid(String lclx) {
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        List<Map<String, Object>> detpList = ttQuestionCollectService.querydetp(sysUser.getId().toString());

        if (null == detpList || detpList.isEmpty()) {
            return Result.error("当前登录用户未设置部门，请设置部门！");
        }
        log.info("当前用户部门信息：" + detpList.get(0).get("DEPART_NAME") + "-" + detpList.get(0).get("ID"));
        String bmid = (String) detpList.get(0).get("ID");
        // 查询出当前用户所在部门默认设置的流程,lclx判断是问题审批类型还是方案审批
        String splcidString = ttQuestionCollectService.querysplcid(bmid,lclx);
        if (oConvertUtils.isEmpty(splcidString)) {
            return Result.error("当前部门未设置默认流程，请为当前部门添加流程！");
        }
        log.info("当前用户流程：" + splcidString);
        return Result.OK(splcidString);
    }



    @AutoLog(value = "获取当前登录用户角色权限")
    @ApiOperation(value = "获取当前登录用户角色权限", notes = "获取当前登录用户角色权限")
    @GetMapping(value = "/getuserroleqx")
    public Result<?> getUserRoleQx() {
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>>  qxxx  =  ttQuestionCollectService.getuserrole(sysUser.getId());
        qxxx =  TransUntil.transformLowerCase(qxxx);
        return Result.OK(qxxx);
    }


    @AutoLog(value = "查询当前用户待审批的任务数量")
    @ApiOperation(value = "查询当前用户待审批的任务数量", notes = "查询当前用户待审批的任务数量")
    @GetMapping(value = "/queryuserdspcount")
    public Result<?> queryUserDspCount() {
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        String counts  =  ttQuestionCollectService.queryuserdspcount(sysUser.getId());
        return Result.OK(counts);
    }



    /**
     * 问题审批列表
     *
     * @param tspmodel
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "问题审批列表-分页列表查询")
    @ApiOperation(value = "问题审批列表-分页列表查询", notes = "问题审批列表-分页列表查询")
    @RequestMapping(value = "/splist", method = RequestMethod.GET)
    public Result<Page<tspmodel>> queryspPageList(tspmodel tspmodel,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<Page<tspmodel>> result = new Result<>();
        Page<tspmodel> pageList = new Page<tspmodel>(pageNo, pageSize);
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        tspmodel.setBy3(sysUser.getId());
        pageList = ttQuestionCollectService.queryquestionsp(pageList, tspmodel);
        result.setSuccess(true);
        // 判断到当前哪个流程节点是否该此人审批
        List<String> adt = ttAuditRecordService.TtAuditRecordSp("wtlx");
        if (null != pageList && pageList.getRecords().size() > 0 && null != adt && adt.size() > 0) {
            pageList.getRecords().get(0).setBy2(adt.get(0));
        }
        result.setResult(pageList);
        return result;
    }


    /**
     * 查询更新审核人审批状态
     *
     * @param id
     * @return
     */
	/*
	@AutoLog(value = "查询更新审核人审批状态")
	@ApiOperation(value = "查询更新审核人审批状态", notes = "查询更新审核人审批状态")
	@GetMapping(value = "/querybytableid")
	public Result<?> queryshztById(@RequestParam(name = "id", required = true) String id) {
	//	List<Map<String, Object>> list = ttAuditRecordService.queryshztgx(id);
		list = TransUntil.transformLowerCase(list);
		if (list == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(list);
	}
	*/

    /**
     * 通过tableid查询
     *
     * @param tableid
     * @return
     */
    @AutoLog(value = "问题收集表-通过tableid查询")
    @ApiOperation(value = "问题收集表-通过tableid查询", notes = "问题收集表-通过tableid查询")
    @GetMapping(value = "/querybytableid")
    public Result<?> queryTableidById(@RequestParam(name = "tableid", required = true) String tableid,
                                      @RequestParam(name = "lclx", required = true) String lclx
    ) {
        List<Map<String, Object>> list = ttAuditRecordService.getshjl(tableid,lclx);
        list = TransUntil.transformLowerCase(list);
        if (list == null) {
            return Result.error("未找到对应数据");
        }

        return Result.OK(list);
    }

    /**
     * @param ttQuestionCollect
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "问题处理表-分页列表查询")
    @ApiOperation(value = "问题处理表-分页列表查询", notes = "问题处理表-分页列表查询")
    @RequestMapping(value = "/cllist", method = RequestMethod.GET)
    public Result<Page<TtQuestionCollect>> queryclPageList(TtQuestionCollect ttQuestionCollect,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<Page<TtQuestionCollect>> result = new Result<Page<TtQuestionCollect>>();

        Page<TtQuestionCollect> pageList = new Page<TtQuestionCollect>(pageNo, pageSize);
        pageList = ttQuestionCollectService.queryquestioncl(pageList, ttQuestionCollect);
        result.setSuccess(true);
        if (null != pageList && pageList.getRecords().size() > 0) {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            pageList.getRecords().get(0).setBy1(sysUser.getUsername());
        }
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param ttQuestionCollect
     * @return
     */
    @AutoLog(value = "问题收集表-添加")
    @ApiOperation(value = "问题收集表-添加", notes = "问题收集表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody TtQuestionCollect ttQuestionCollect) {

//    	if (ttQuestionCollect.getQuestioncontent().length()>375) {
//			return Result.error("问题详细内容过长,请上传附件进行说明！");
//		}
        // 获取用户信息 添加收集人为当前登录用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //添加自定义问题编号
        String code = ttQuestionCollectService.cxxl();

        String wtidcode = TransUntil.wtcode(ttQuestionCollect, code);
        ttQuestionCollect.setQuestioncode(wtidcode);

        ttQuestionCollect.setCreateById(sysUser.getId());
        ttQuestionCollect.setCollectorname(sysUser.getRealname());

        //默认方案审批状态为未提交
        ttQuestionCollect.setFashbj("0");

        // 默认流程状态为未提交
        ttQuestionCollect.setFlowstatus("0");
        // 默认处理状态为未处理
        ttQuestionCollect.setDealstatus("待办");
        // 默认审批状态为未审批
        ttQuestionCollect.setAuditstatus("0");
        //默认是否转让为否
        ttQuestionCollect.setSfzrother("2");
        ttQuestionCollect.setScbj("0");

        if (!"HZB".equals(ttQuestionCollect.getProname())){
            ttQuestionCollect.setProject("无");
        }


        ttQuestionCollectService.save(ttQuestionCollect);
        return Result.OK("添加成功！",ttQuestionCollect.getId());
    }




    /**
     * 编辑是否出具方案-是否同步jira
     *
     * @param ttQuestionCollect
     * @return
     */
    @AutoLog(value = "问题收集表-编辑是否出具方案")
    @ApiOperation(value = "问题收集表-编辑是否出具方案", notes = "问题收集表-编辑是否出具方案")
    @PutMapping(value = "/editsfcjfa")
    public Result<?> editSfCjfa(@RequestBody TtQuestionCollect ttQuestionCollect) {

        if ("2".equals(ttQuestionCollect.getSfcfabj())){
            ttQuestionCollect.setFashbj("2");
            //添加一条空方案
            TtPlanConfirm ttPlanConfirm = ttPlanConfirmService.getbyquestionid(ttQuestionCollect.getId());
            if(ttPlanConfirm==null){
                TtPlanConfirm ttplanconfirm = new TtPlanConfirm();
                ttplanconfirm.setPlanQuestionid(ttQuestionCollect.getId());
                ttPlanConfirmService.save(ttplanconfirm);
            }
        }

        ttQuestionCollectService.updateById(ttQuestionCollect);
        return Result.OK("编辑成功!",ttQuestionCollect.getId());
    }




    /**
     * 编辑
     *
     * @param ttQuestionCollect
     * @return
     */
    @AutoLog(value = "问题收集表-编辑")
    @ApiOperation(value = "问题收集表-编辑", notes = "问题收集表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TtQuestionCollect ttQuestionCollect) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if ("0".equals(ttQuestionCollect.getAuditstatus()) || "3".equals(ttQuestionCollect.getAuditstatus())) {
            ttQuestionCollect.setAuditstatus("0");
            ttQuestionCollect.setIsjira("0");
            ttQuestionCollect.setDealstatus("待办");
            ttQuestionCollect.setCreateById(sysUser.getId());
        }
        ttQuestionCollectService.updateById(ttQuestionCollect);
        return Result.OK("编辑成功!",ttQuestionCollect.getId());
    }

    /**
     * 编辑
     *
     * @param ttQuestionCollect
     * @return
     */
    @AutoLog(value = "问题收集表-编辑")
    @ApiOperation(value = "问题收集表-编辑", notes = "问题收集表-编辑")
    @PutMapping(value = "/editspjm")
    public Result<?> editspjm(TtQuestionCollect ttQuestionCollect) {
        ttQuestionCollectService.updateById(ttQuestionCollect);
        return Result.OK("编辑成功!");
    }

    /**
     * 审核
     *
     * @param id
     * @param shbj
     * @param auditoption
     * @return
     */
    @AutoLog(value = "审核")
    @ApiOperation(value = "审核", notes = "问题审核")
    @PutMapping(value = "/questionsh")
    public Result<?> edit(@RequestParam(name = "id", required = true) String id,
                          @RequestParam(name = "shbj", required = true) String shbj,
                          @RequestParam(name = "auditoption", required = true) String auditoption,
                          @RequestParam(name = "spyijian", required = false) String spyijian) {
        String lclx = "wtlx";//问题流程
        return ttAuditRecordService.audit(id, shbj, auditoption,lclx,spyijian);
    }


    /**
     * 审核
     *
     * @param id
     * @param shbj
     * @param auditoption
     * @return
     */
    @AutoLog(value = "查询下一级审批人有无问题提出人")
    @ApiOperation(value = "查询下一级审批人有无问题提出人", notes = "查询下一级审批人有无问题提出人")
    @PutMapping(value = "/querynextspr")
    public Result<?> querynextspr(@RequestParam(name = "id", required = true) String id) {
        String lclx = "wtlx";//问题流程
        return ttAuditRecordService.querynextaudit(id,lclx);
    }


    /**
     * 方案审核
     *
     * @param id
     * @param fashbj
     * @param auditoption
     * @return
     */
    @AutoLog(value = "方案审核")
    @ApiOperation(value = "方案审核", notes = "方案审核")
    @PutMapping(value = "/fangansh")
    public Result<?> fash(@RequestParam(name = "id", required = true) String id,
                          @RequestParam(name = "fashbj", required = true) String fashbj,
                          @RequestParam(name = "auditoption", required = true) String auditoption) {

        String lclx = "falc";//方案流程
        return ttAuditRecordService.fasp(id, fashbj, auditoption,lclx);
    }


    /**
     * 通过id删除
     *scbj删除
     * @param id
     * @return
     */
    @AutoLog(value = "问题收集表-通过id删除")
    @ApiOperation(value = "问题收集表-通过id删除", notes = "问题收集表-通过id删除")
    @PutMapping(value = "/deletebyscbj")
    public Result<?> delete(@RequestBody TtQuestionCollect ttQuestionCollect) {
        ttQuestionCollectService.updatescbjById(ttQuestionCollect.getId());
        //ttQuestionCollectService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "问题收集表-批量删除")
    @ApiOperation(value = "问题收集表-批量删除", notes = "问题收集表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ttQuestionCollectService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过questioncode查询问题处理记录
     *
     * @param questioncode
     * @return
     */
    @AutoLog(value = "问题处理表-通过questioncode查询")
    @ApiOperation(value = "问题处理表-通过questioncode查询", notes = "问题处理表-通过questioncode查询")
    @GetMapping(value = "/queryBycode")
    public Result<?> queryByquestioncode(@RequestParam(name = "questioncode", required = true) String questioncode) {
        TtQuestionDealRecord ttQuestionDealRecord = ttQuestionCollectService.querybyquestioncode(questioncode);
        if (ttQuestionDealRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(ttQuestionDealRecord);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "问题收集表-通过id查询")
    @ApiOperation(value = "问题收集表-通过id查询", notes = "问题收集表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        TtQuestionCollect ttQuestionCollect = ttQuestionCollectService.getById(id);
        if (ttQuestionCollect == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(ttQuestionCollect);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ttQuestionCollect
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TtQuestionCollect ttQuestionCollect) {
        return super.exportXls(request, ttQuestionCollect, TtQuestionCollect.class, "问题收集表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TtQuestionCollect.class);
    }

}
